import * as maptalks from "maptalks";

class measure {

  /**
   *  开始测量
   * @param targetTool  distanceTool 或者  areaTool 对象
   */
  ctStart(targetTool) {
    targetTool.enable()
  }

  /**
   * 结束测量
   * @param targetTool  distanceTool 或者  areaTool 对象
   */
  ctFinish(targetTool) {
    targetTool.disable()
  }

  /**
   * 测量距离
   * @param map
   * @returns {*}
   */
  ctCreateDistanceTool() {
    return new maptalks.DistanceTool({
      once: true,
      'language': 'zh-CN',
      'symbol': {
        'lineColor': '#34495e',
        'lineWidth': 2
      },
      'vertexSymbol': {
        'markerType': 'ellipse',
        'markerFill': '#1bbc9b',
        'markerLineColor': '#000',
        'markerLineWidth': 3,
        'markerWidth': 10,
        'markerHeight': 10
      },

      'labelOptions': {
        'textSymbol': {
          'textFaceName': 'monospace',
          'textFill': '#fff',
          'textLineSpacing': 1,
          'textHorizontalAlignment': 'right',
          'textDx': 15,
          'markerLineColor': '#b4b3b3',
          'markerFill': '#000'
        },
        'boxStyle': {
          'padding': [6, 2],
          'symbol': {
            'markerType': 'square',
            'markerFill': '#000',
            'markerFillOpacity': 0.9,
            'markerLineColor': '#b4b3b3'
          }
        }
      },
      'clearButtonSymbol': [{
        'markerType': 'square',
        'markerFill': '#000',
        'markerLineColor': '#b4b3b3',
        'markerLineWidth': 2,
        'markerWidth': 15,
        'markerHeight': 15,
        'markerDx': 20
      }, {
        'markerType': 'x',
        'markerWidth': 10,
        'markerHeight': 10,
        'markerLineColor': '#fff',
        'markerDx': 20
      }]
    })
  }

  /**
   * 测量面积
   * @param mode 默认多边形  'Polygon'
   */
  ctCreateAreaTool(mode) {
    return new maptalks.AreaTool({
      language: 'zh-CN',
      mode: mode,
      once: true,
      symbol: {
        'lineColor': '#1bbc9b',
        'lineWidth': 2,
        'polygonFill': '#fff',
        'polygonOpacity': 0.3
      },
      vertexSymbol: {
        'markerType': 'ellipse',
        'markerFill': '#34495e',
        'markerLineColor': '#1bbc9b',
        'markerLineWidth': 3,
        'markerWidth': 10,
        'markerHeight': 10
      },
      labelOptions: {
        'textSymbol': {
          'textFaceName': 'monospace',
          'textFill': '#fff',
          'textLineSpacing': 1,
          'textHorizontalAlignment': 'right',
          'textDx': 15
        },
        boxStyle: {
          'padding': [6, 2],
          'symbol': {
            'markerType': 'square',
            'markerFill': '#000',
            'markerFillOpacity': 0.9,
            'markerLineColor': '#b4b3b3'
          }
        }
      },
      clearButtonSymbol: [{
        'markerType': 'square',
        'markerFill': '#000',
        'markerLineColor': '#b4b3b3',
        'markerLineWidth': 2,
        'markerWidth': 15,
        'markerHeight': 15,
        'markerDx': 22
      }, {
        'markerType': 'x',
        'markerWidth': 10,
        'markerHeight': 10,
        'markerLineColor': '#fff',
        'markerDx': 22
      }],
    })
  }

  ctAddToMap(measureTool, map) {
    measureTool.addTo(map).disable();
  }

  /**
   * 添加 测量  事件监听
   * @param measureTool  distanceTool / areaTool
   * @param event
   * @param callback
   */
  ctAddListen(measureTool, event, callback) {
    measureTool.on(event, (param) => {
      callback(param)
    });
  }
}

export default new measure()
